<!-- src/views/stocktaking/ReportLoss.vue -->
<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

// 接收传入的行数据
const props = defineProps({
  rowData: {
    type: Object,
    required: true
  }
})

// 损失类型选项
const lossCategoryOptions = [
  { label: '破损', value: 1 },
  { label: '丢失', value: 2 }
]

// 表单数据
const formData = ref({
  lossCategory: null
})

// 表单引用
const formRef = ref()

// 验证规则
const rules = {
  lossCategory: [
    { required: true, message: '请选择损失类型', trigger: 'change' }
  ]
}

// 提交表单
const handleSubmit = async (callback) => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      // 构造提交数据
      const submitData = {
        lossCount: props.rowData.discrepancyCount, // 差异数量
        lossCategory: formData.value.lossCategory, // 损失类型
        stocktakingInfo: props.rowData.id // 盘点信息ID
      }
      
      // 调用回调函数传递数据
      callback(submitData)
    } else {
      ElMessage.warning('请选择损失类型')
    }
  })
}

// 暴露方法给父组件
defineExpose({
  handleSubmit
})
</script>

<template>
  <div class="report-loss-container">
    <el-form 
      ref="formRef" 
      :model="formData" 
      :rules="rules" 
      label-width="120px"
      @submit.prevent
    >
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="商品名称">
            <el-input v-model="rowData.title" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="差异数量">
            <el-input v-model="rowData.discrepancyCount" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="损失类型" prop="lossCategory">
            <el-select 
              v-model="formData.lossCategory" 
              placeholder="请选择损失类型"
              style="width: 100%"
            >
              <el-option 
                v-for="item in lossCategoryOptions" 
                :key="item.value" 
                :label="item.label" 
                :value="item.value" 
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<style scoped>
.report-loss-container {
  padding: 20px;
}

:deep(.el-input.is-disabled .el-input__inner) {
  background-color: #f5f7fa;
  color: #606266;
}
</style>